/*
 * Copyright 2010 Aekold Helbrass
 *
 * This file is part of RosterComposer.
 *
 * RosterComposer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * RosterComposer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with RosterComposer.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.heresylabs.rostercomposer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import org.heresylabs.rostercomposer.roster.RosterEntry;

/**
 *
 * @author Aekold Helbrass <Helbrass@gmail.com>
 */
public class RosterControlPanel extends JPanel {

    private final JTable rosterTable;

    private final Composer composer;

    public RosterControlPanel(JTable rosterTable, Composer composer) {
        this.rosterTable = rosterTable;
        this.composer = composer;

        super.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
        super.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JComponent[] components = new JComponent[]{createListManipulationPanel(), new Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(0, Short.MAX_VALUE)), composer.getTotalPointsLabel()};
        for (int i = 0; i < components.length; i++) {
            JComponent component = components[i];
            component.setAlignmentX(0f);
            super.add(component);
        }
    }

    private JPanel createListManipulationPanel() {
        JPanel buttonsPanel = new JPanel(new GridLayout(0, 1, 6, 6));

        buttonsPanel.add(new JButton(new DeleteAction()));
        buttonsPanel.add(new JButton(new MoveUpAction()));
        buttonsPanel.add(new JButton(new MoveDownAction()));

        buttonsPanel.setMaximumSize(new Dimension(buttonsPanel.getMaximumSize().width, buttonsPanel.getPreferredSize().height));

        return buttonsPanel;
    }

    private JPanel createTotalPointsPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(composer.getTotalPointsLabel(), BorderLayout.CENTER);
        return panel;
    }

    private int getSelectedRow() {
        int row = rosterTable.getSelectedRow();
        if (row < 0) {
            return -1;
        }
        return rosterTable.convertRowIndexToModel(row);
    }

    private class DeleteAction extends AbstractAction {

        public DeleteAction() {
            putValue(NAME, "<html>Delete&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"); // it's to make label the same width as "Move Down"
            putValue(SMALL_ICON, new ImageIcon(getClass().getResource("images/edit-delete.png")));
            putValue(SHORT_DESCRIPTION, "Delete selected roster entries");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int row = getSelectedRow();
            if (row < 0) {
                return;
            }

            RosterEntry entry = composer.getRosterEntry(row);

            JOptionPane confirm = new JOptionPane();
            confirm.setOptions(new Object[]{"Yes", "No"});
            confirm.setInitialValue("No");
            confirm.setMessageType(JOptionPane.QUESTION_MESSAGE);
            confirm.setMessage("Are you sure you want to delete " + entry.getUnit().getName() + " from your roster?");

            confirm.createDialog(rosterTable, "Are you sure?").setVisible(true);
            if ("Yes".equals(confirm.getValue())) {
                composer.deleteRosterEntry(row);
            }
        }

    }

    private class MoveUpAction extends AbstractAction {

        public MoveUpAction() {
            putValue(NAME, "<html>Move Up&nbsp;&nbsp;&nbsp;&nbsp;"); // it's to make label the same width as "Move Down"
            putValue(SMALL_ICON, new ImageIcon(getClass().getResource("images/arrow-up.png")));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int row = getSelectedRow();
            if (row <= 0) {
                return;
            }

            composer.moveUp(row);
            rosterTable.getSelectionModel().clearSelection();
            rosterTable.getSelectionModel().addSelectionInterval(row - 1, row - 1);
        }

    }

    private class MoveDownAction extends AbstractAction {

        public MoveDownAction() {
            putValue(NAME, "Move Down");
            putValue(SMALL_ICON, new ImageIcon(getClass().getResource("images/arrow-down.png")));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int row = getSelectedRow();
            int newRow = row + 1;
            if (row < 0 || newRow >= rosterTable.getRowCount()) {
                return;
            }

            composer.moveDown(row);
            rosterTable.getSelectionModel().clearSelection();
            rosterTable.getSelectionModel().addSelectionInterval(newRow, newRow);
        }

    }

}
